package cn.iocoder.yudao.module.system.controller.admin.price;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.price.vo.LivePriceConfPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.price.vo.LivePriceConfRespVO;
import cn.iocoder.yudao.module.system.controller.admin.price.vo.LivePriceConfSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.price.LivePriceConfDO;
import cn.iocoder.yudao.module.system.service.price.LivePriceConfService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 实时价格配置")
@RestController
@RequestMapping("/system/live-price-conf")
@Validated
public class LivePriceConfController {

    @Resource
    private LivePriceConfService livePriceConfService;

    @PostMapping("/create")
    @Operation(summary = "创建实时价格配置")
    @PreAuthorize("@ss.hasPermission('system:live-price-conf:create')")
    public CommonResult<Long> createLivePriceConf(@Valid @RequestBody LivePriceConfSaveReqVO createReqVO) {
        return success(livePriceConfService.createLivePriceConf(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新实时价格配置")
    @PreAuthorize("@ss.hasPermission('system:live-price-conf:update')")
    public CommonResult<Boolean> updateLivePriceConf(@Valid @RequestBody LivePriceConfSaveReqVO updateReqVO) {
        livePriceConfService.updateLivePriceConf(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除实时价格配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:live-price-conf:delete')")
    public CommonResult<Boolean> deleteLivePriceConf(@RequestParam("id") Long id) {
        livePriceConfService.deleteLivePriceConf(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得实时价格配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:live-price-conf:query')")
    public CommonResult<LivePriceConfRespVO> getLivePriceConf(@RequestParam("id") Long id) {
        LivePriceConfDO livePriceConf = livePriceConfService.getLivePriceConf(id);
        return success(BeanUtils.toBean(livePriceConf, LivePriceConfRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得实时价格配置分页")
    @PreAuthorize("@ss.hasPermission('system:live-price-conf:query')")
    public CommonResult<PageResult<LivePriceConfRespVO>> getLivePriceConfPage(@Valid LivePriceConfPageReqVO pageReqVO) {
        PageResult<LivePriceConfDO> pageResult = livePriceConfService.getLivePriceConfPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LivePriceConfRespVO.class));
    }
    @GetMapping("/list")
    @Operation(summary = "获得实时价格配置")
    @PermitAll
    public CommonResult<List<LivePriceConfDO>> getLivePriceConfList() {
        return success(livePriceConfService.getLivePriceConfList());
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出实时价格配置 Excel")
    @PreAuthorize("@ss.hasPermission('system:live-price-conf:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportLivePriceConfExcel(@Valid LivePriceConfPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LivePriceConfDO> list = livePriceConfService.getLivePriceConfPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "实时价格配置.xls", "数据", LivePriceConfRespVO.class,
                        BeanUtils.toBean(list, LivePriceConfRespVO.class));
    }

}